<template>
	<view>
		<!-- 首页图片 -->
		<RxHomeImage @searchContext='searchContexts'></RxHomeImage>
		
		<!-- 头 -->
		<RxTou @reload='reloads'></RxTou>
		
		<!-- 数据列表 -->
		<RxHomeList :RxData="datalist" :RxLoadingStatus="loading_status"></RxHomeList>
		
		<!-- 个人中心弹出框 -->
		<RxMyPopup></RxMyPopup>
	</view>
</template>

<script>
	import RxHomeImage from "@/components/RxHomeImage/RxHomeImage.vue"
	import RxTou from '@/components/RxTou/RxTou.vue'
	import RxHomeList from "@/components/RxHomeList/RxHomeList.vue"
	import RxMyPopup from "@/components/RxMyPopup/RxMyPopup.vue"
	export default {
		components: {
			RxHomeImage, RxTou, RxHomeList, RxMyPopup
		},
		data() {
			return {
				userinfo: {},
				has_more: false,		// 是否有更多数据
				datalist: [],
				search: '',
				pageIndex: 1,      //第几页
				loading_status: 'loadmore'
			}
		},
		created() {
			this.userinfo = this.$store.state.userinfo
			this.getRxList()
		},
		// 上拉触底事件
		onReachBottom:function( ) {
			if (this.has_more) {
				this.getRxList()  //调用函数
				setTimeout(function () {
					uni.stopPullDownRefresh();
				}, 1000);  
			}
		},
		methods: {
			searchContexts(item) {
				this.search = item.data
				this.reloads()
			},
			reloads() {
				this.pageIndex = 1
				this.datalist = []
				this.getRxList()
			},
			getRxList() {
				var _this = this
				_this.loading_status = 'loading'
				
				_this.$api.get_rx({  //以下是pagedQuery云函数需要传入的参数
						filter:{ clinic: _this.userinfo.clinic_id_list, search: _this.search },          //筛选条件，格式["clinicId"]
						pageIndex: _this.pageIndex,      //第几页
						types: _this.userinfo.types,	// 用户类型
						userId: _this.userinfo.userId,
						name: _this.userinfo.name,
						mobile: _this.userinfo.mobile,
						self_list: _this.userinfo.self
				}).then((res) => {
					console.log('接收到列表数据', res)
					_this.datalist = _this.datalist.concat(res.data.data)  //云端返回的查询数据,追加到数组datalist
					_this.has_more = res.data.has_more  //云端返回的是否有更多数据
					if (_this.has_more) {
						_this.pageIndex = _this.pageIndex + 1
						_this.loading_status = 'loadmore'
					} else {
						_this.loading_status = 'nomore'
					}
				}).catch(err => {
					console.log('错误', err)
					uni.showToast({ title: '服务错误', icon: 'error', duration: 3000 })
				});
			}
		}
	}
</script>

<style>
	
</style>
